package com.droidnova.android.games.vortex;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.StringTokenizer;

import android.app.Application;
import android.content.Context;
import android.os.Environment;
import android.util.Log;

public class export extends Application {
	
	public static final String LOG_TAG = "Droidnova";

	int _noVertices = 0;
	float _intensity[]=null ;
	private String _new[]=null,_even[]=null,_odd[]=null, _points[]=null;
	float max_value=0.0f;
	float cr = 0.9f,cg=0.3f ,ca=1.0f;
	int number=0;
	float _pcolors[]=null , _pvertices[]=null; 
	//calculate color values for each point
	int count=0;
	float i_value=0.0f,cb = 0.0f;
  
	 
	

	public export(Context ctx,String filename)
	{
		Log.w("MyClassName", "points");
		if(filename==null)
		{
		createVC(null);
		}
		else
		{
		createVC(filename);	
		}
		

	}
	
	public void createVC(String fname)
	{
		_noVertices = 0;
		_intensity=null ;
		_new=null;
		_even=null;
		_odd=null;
		_points=null;
		max_value=0.0f;
		cr = 0.9f;
		cg=0.3f;
		ca=1.0f;
		number=0;
		_pcolors=null;
		_pvertices=null; 
		//calculate color values for each point
		count=0;
		i_value=0.0f;
		cb = 0.0f;
		
		File sdcard = Environment.getExternalStorageDirectory();
		File file = null;
		if(fname==null)
		{
			file = new File(sdcard,"input.txt");
		}
		else
		{
		file = new File(sdcard,fname);
		}
        	//InputStream fIn = ctx.getResources().openRawResource(R.raw.input);	
       
		{
			BufferedReader br = null;
			try {
				br = new BufferedReader(new FileReader(file));
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
            //Log.v("input stream","input stream->"+fIn.toString());
			//InputStreamReader isr = new InputStreamReader(fIn);
			//BufferedReader br = new BufferedReader(isr);
			String stringRead = null;

			ArrayList <String> words = new ArrayList<String> ();
			ArrayList <String> points = new ArrayList<String>();

			ArrayList<String> even = new ArrayList<String>();
			ArrayList<String> odd = new ArrayList<String>();



			try {
				stringRead = br.readLine( );
			} catch (IOException e) {

				e.printStackTrace();
			}
			while( stringRead != null ) // end of the file?
			{

				StringTokenizer st = new StringTokenizer(stringRead,",");

				while(st.hasMoreTokens())
				{
					words.add(st.nextToken());

				}

				try {
					stringRead = br.readLine( );
				} catch (IOException e) {

					e.printStackTrace();
				}
				_new = words.toArray(new String[words.size()]);

			}


			for(int i=0;i<_new.length;i+=2)
			{
				even.add(_new[i]);	
			}

			for(int i=1;i<_new.length;i+=2)
			{
				odd.add(_new[i]);	
			}
			_even = even.toArray(new String[even.size()]);
			_odd = odd.toArray(new String[odd.size()]);


			for(int i=0 ;i < _even.length;i++)
			{
				StringTokenizer st = new StringTokenizer(_even[i], " "); 
				while(st.hasMoreTokens())
				{
					points.add(st.nextToken()+ "f");

				}
			}
			_points = points.toArray(new String[points.size()]);
			try{
				_pvertices = convertStringArraytoFloatArray(_points);
				_intensity = convertStringArraytoFloatArray(_odd);
			} catch (Exception e) {

				e.printStackTrace();

			}


		}
		
		_noVertices = _intensity.length ;
		 number = _intensity.length *4;
         
		Log.v("NUMBER", "FOUND NUMBER -> " + number);
		_pcolors = new float[number];
		max_value = getMaxValue(_intensity);
		for(int i = 0;i<_intensity.length;i++)
		{		
			i_value = _intensity[i];
			cb = i_value/max_value;
			
			/*for(int j=0;j<=3;j++)
			{
				if(j==0)
				{
					_pcolors[count]=cr;	
				}
				else if(j==1)
				{
					_pcolors[count]=cg;	
				}
				else if(j==2)
				{
					_pcolors[count]=cb;
				}
				else
				{
					_pcolors[count]=ca;	
				}
				count++;
			}*/
			
			
			//color of values
			
			if(0 <= cb && cb < 0.25f)
			{
				cr = 1.0f;
				cg=0.8f;//cg = 1.0f;
				cb=0.6f;//cb = 0.0f;	
			}
			else if(0.25f <= cb && cb < 0.50f)
			{
//				cr = 0.0f;
//				cg = 1.0f;
//				cb = 1.0f;
				cr=0.0f;
				cg=0.0f;
				cb=1.0f;
			}
			else if(0.50f <= cb && cb < 0.75f)
			{
//				cr = 1.0f;
//				cg = 0.0f;
//				cb = 1.0f;
				cr = 0.0f;
				cg = 1.0f;
				cb = 0.0f;
			}
			else
			{
				//cr = 1.0f;
				//cg = 1.0f;
				//cb = 0.0f;
//				cr = 1.0f;
//				cg = 1.0f;
//				cb = 1.0f;
				cr = 1.0f;
				cg = 0.0f;
				cb = 0.0f;
			}
			
			for(int j=0;j<=3;j++)
			{
				if(j==0)
				{
					_pcolors[count]=cr;	
				}
				else if(j==1)
				{
					_pcolors[count]=cg;	
				}
				else if(j==2)
				{
					_pcolors[count]=cb;
				}
				else
				{
					_pcolors[count]=ca;	
				}
				count++;
			}
			
			
		}
	}
	
	//conversion ofstring array to float array	
	public float[] convertStringArraytoFloatArray(String[] _new) throws Exception 
	{
		if (_new != null)
		{
			float floatarray[] = new float[_new.length];
			for (int i = 0; i < _new.length; i++) {
				floatarray[i] = Float.parseFloat(_new[i]);
			}
			return floatarray;
		}
		return null;
	}
	// Finding the maximum value in an array
	public float getMaxValue(float[] numbers){  
		float maxValue = numbers[0];  
		for(int i=1;i < numbers.length;i++){  
			if(numbers[i] > maxValue){  
				maxValue = numbers[i];
			}  
		}  
		return maxValue;  
	}  
	
	// Finding the maximum x value in an array
	public float getMaxXValue(float[] numbers){  
		float maxValue = numbers[0];  
		for(int i=3;i < numbers.length;i+=3){  
			if(numbers[i] > maxValue){  
				maxValue = numbers[i];
			}  
		}  
		return maxValue;  
	}  

}
